Skip to contentMethod: static {...}
1: /**
2: * Copyright (C) 2020 Czech Technical University in Prague
3: * <p>
4: * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by
5: * the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
6: * <p>
7: * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
8: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a
9: * copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
10: */
11: package cz.cvut.kbss.jsonld.serialization.traversal;
12:
13: import cz.cvut.kbss.jsonld.common.BeanAnnotationProcessor;
14: import cz.cvut.kbss.jsonld.common.BeanClassProcessor;
15:
16: import java.util.Collection;
17: import java.util.Map;
18: import java.util.Objects;
19:
20: /**
21: * Serializes a {@link cz.cvut.kbss.jopa.model.annotations.Properties} field.
22: * <p>
23: * Note that at the moment, when the map also contains a property which is already mapped by another field, a conflict in
24: * the resulting JSON-LD will arise.
25: */
26: class PropertiesTraverser {
27:
28: private final ObjectGraphTraverser parent;
29:
30: PropertiesTraverser(ObjectGraphTraverser parent) {
31: this.parent = parent;
32: }
33:
34: public void traverseProperties(SerializationContext<Map<?, ?>> ctx) {
35: for (Map.Entry<?, ?> e : ctx.getValue().entrySet()) {
36: final String property = e.getKey().toString();
37: if (e.getValue() == null) {
38: continue;
39: }
40: if (e.getValue() instanceof Collection) {
41: final Collection<?> propertyValues = (Collection<?>) e.getValue();
42: serializePropertyValues(property, propertyValues);
43: } else {
44: visitSingleValue(property, e.getValue());
45: }
46: }
47: }
48:
49: private void visitSingleValue(String property, Object value) {
50: assert value != null;
51: if (isTraversable(value)) {
52: parent.traverseSingular(new SerializationContext<>(property, value));
53: } else {
54: parent.visitAttribute(new SerializationContext<>(property, value));
55: }
56: }
57:
58: private static boolean isTraversable(Object value) {
59: final Class<?> cls = value.getClass();
60: return (BeanClassProcessor.isIdentifierType(value.getClass()) && !String.class.equals(cls)) ||
61: BeanAnnotationProcessor.isOwlClassEntity(value.getClass()) ||
62: BeanAnnotationProcessor.hasTypesField(cls);
63: }
64:
65: private void serializePropertyValues(String property, Collection<?> values) {
66: if (values.isEmpty()) {
67: return;
68: }
69: if (values.size() == 1) {
70: final Object val = values.iterator().next();
71: if (val != null) {
72: visitSingleValue(property, val);
73: }
74: } else {
75: final SerializationContext<Collection<?>> colContext = new SerializationContext<>(property, values);
76: parent.openCollection(colContext);
77: values.stream().filter(Objects::nonNull).forEach(v -> visitSingleValue(null, v));
78: parent.closeCollection(colContext);
79: }
80: }
81: }